<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/9
 * Time: 11:26
 */

namespace Game\Services;
use Game\Traits\Singleton;
use Workerman\MySQL\Connection;
use Game\Library\Mysql;

class MysqlService
{
    use Singleton;

    /** @var Connection */
    private $mysql;
    private function mysql(): Connection{
        if ($this->mysql instanceof Connection) {
            return $this->mysql;
        } else {
            return Mysql::getInstance();
        }
    }

    /**
     * 扣除房卡或退还房卡
     * @param $room_id
     * @param $account_id
     * @param $spend_ticket_count
     */
    public function balanceTicket($room_id, $account_id, $spend_ticket_count){
        $timestamp = time();
        $this->mysql()->update(Room_Ticket)->set("ticket_count", "ticket_count-".$spend_ticket_count)->where("account_id=".$account_id)->query();

        //获取流水账
        $balance_where = 'account_id='.$account_id.' and is_delete=0';
        $balance = $this->mysql()->select("balance")->from(Room_Ticket_Journal)->where($balance_where)->orderByDESC(["journal_id"])->single();
        $balance = $balance ? $balance : 0;

        //添加到流水账
        $journal_array['create_time'] = $timestamp;
        $journal_array['create_appid'] = "aid_".$account_id;
        $journal_array['update_time'] = $timestamp;
        $journal_array['update_appid'] = "aid_".$account_id;
        $journal_array['is_delete'] = G_CONST::IS_FALSE;
        $journal_array['account_id'] = $account_id;
        $journal_array['object_id'] = $room_id;
        $journal_array['object_type'] = 3;  //游戏

        $journal_array['extra'] = "";

        if($spend_ticket_count > 0){
            $journal_array['disburse'] = $spend_ticket_count;
            $journal_array['abstract'] = "炸金花";		//摘要
        } else {
            $journal_array['income'] = -$spend_ticket_count;
            $journal_array['abstract'] = "炸金花房卡退还";		//摘要
        }

        $journal_array['balance'] = $balance - $spend_ticket_count;
        if($journal_array['balance'] < 0)
        {
            $this->writeLog("balance negative balance: ".$balance." account_id: ".$account_id." room_id: ".$room_id);
            $journal_array['balance'] = 0;
        }
        $this->mysql()->insert(Room_Ticket_Journal)->cols($journal_array)->query();
    }



    /**
     * 游戏维护公告
     * @param $account_id
     * @return bool
     */
    public function getGameAnnouncement($account_id) {
        $timestamp = time();
        $game_type = Config::Game_Type;
        $announcement_where = 'game_type='.$game_type.' and announce_time<='.$timestamp.' and end_time>'.$timestamp.' and is_delete=0';
        $announcement_query = $this->mysql()->select("announce_time,service_time,end_time,announce_text,service_text")
            ->from(Game_Announcement)->where($announcement_where)->query();

        if(is_array($announcement_query) && count($announcement_query) > 0) {
//            $announce_time = $announcement_query[0]['announce_time'];
            $service_time = $announcement_query[0]['service_time'];
//            $end_time = $announcement_query[0]['end_time'];
            $announce_text = $announcement_query[0]['announce_text'];
            $service_text = $announcement_query[0]['service_text'];

            if($timestamp >= $service_time) {
                $array['alert_type'] = 4;
                $array['alert_text'] = $service_text;
            } else {
                $array['alert_type'] = 4;
                $array['alert_text'] = $announce_text;
            }

            //获取白名单用户
            $whilelist_sql = 'select data_id from '.Game_Whilelist.' where account_id='.$account_id.' and is_delete=0';
            $whilelist_query = $this->mysql()->query($whilelist_sql);
            if(is_array($whilelist_query) && count($whilelist_query) > 0)  {
                $this->logMessage('error', "function(getGameAnnouncement):白名单用户:".$account_id." in file".__FILE__." on Line ".__LINE__);
                return true;
            } else {
                return $array;
            }
        }
        return true;
    }

    /**
     * 获取多个用户
     * @param $account_id
     * @param string $fields
     * @return mixed
     */
    public function getAccountListByID($account_id, $fields = "*"){
        if (is_array($account_id)){
            $account_id = implode(",",$account_id);
        }
        return $this->mysql()->from(WX_Account)->select($fields)->where("id in (".$account_id.")")->query();
    }

    public function getAccountByID($account_id, $fields = "*"){
        return $this->mysql()->from(WX_Account)->select($fields)->where("id =".$account_id."")->row();
    }

    /**
     * 房卡数量
     * @param $account_id
     * @return string
     */
    public function getTicketCount($account_id){
        return $this->mysql()->from(Room_Ticket)->select("ticket_count")->where("account_id=".$account_id)->single();
    }

    /**
     * 获取房间
     * @param $data_key
     * @return array
     */
    public function getRoomByDataKey($data_key){
        return $this->mysql()->from(Room)->select("room_id,room_number,account_id,is_close")->where('data_key="'.$data_key.'"')->row();
    }

    /**
     * 获取房间
     * @param $room_number
     * @return array
     */
    public function getRoomByRoomNumber($room_number){
        return $this->mysql()->from(Room)->select("room_id,room_number,account_id,is_close")->where('room_number="'.$room_number.'"')->row();
    }

    /**
     * 创建房间
     * @param $account_id
     * @param $data_key
     * @return array|bool
     */
    public function createRoomData($account_id, $data_key){
        $timestamp = time();
        $r_array['create_time']     = $timestamp;
        $r_array['create_appid']    = $account_id;
        $r_array['update_time']     = $timestamp;
        $r_array['update_appid']    = $account_id;
        $r_array['is_delete']       = G_CONST::IS_FALSE;
        $r_array['data_key']        = $data_key;
        $r_array['account_id']      = $account_id;
        $r_array['is_close']        = G_CONST::IS_FALSE;
        $r_array['game_type']       = Config::Game_Type;
        $room_id = $this->mysql()->insert(Room)->cols($r_array)->query();
        if($room_id > 0){
            $room_number = $room_id . mt_rand(1,9);
            $this->mysql()->update(Room)->where("room_id=".$room_id)->cols(array("room_number"=>$room_number))->query();
            $r_array['room_number'] = $room_number;
            $r_array['room_id'] = $room_id;
            return $r_array;
        } else {
            return false;
        }
    }
    public function updateRoomPassword($room_id, $password){
        return $this->mysql()->update(Room)->where("room_id=".$room_id)->cols(array("password"=>$password))->query();
    }
}